﻿using DotNetty.Framework;
using System;
using System.Diagnostics;
using System.Threading.Tasks;

namespace iTool.HttpRouterPattern
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            string template = "api/products/{productId}/in{sku}fo";
            string request = "api/products/1059/in123fo";
            string queryString = "?order=15&&sku=123&sku=123";


            Stopwatch sw = new Stopwatch();
            sw.Start();
            try
            {
                // 创建路由匹配器
                IRouteTemplatePatternCreator routeTemplatePatternCreator = new RouteTemplatePatternCreator();
                RouteTemplate routeTemplate = routeTemplatePatternCreator.Create(template);

                Parallel.For(0, 10_000_000, index =>
                {
                    // 处理URL参数
                    IUrlPathPlaceholderNameAndValueFinder placeholder = new UrlPathPlaceholderNameAndValueFinder();
                    var parameters = placeholder.Find(request, queryString, template);

                    // 路由匹配
                    IUrlPathToUrlTemplateMatcher matcher = new RegExUrlMatcher();
                    matcher.Match(request, queryString, routeTemplate);
                    //Console.WriteLine("matcher.Match(request,string.Empty, routeTemplate)：{0}", matcher.Match(request, queryString, routeTemplate));
                    //Console.WriteLine("parameters：");
                    //foreach (var item in parameters)
                    //{
                    //    Console.WriteLine("{0}：{1}", item.Name, item.Value);
                    //}
                });
            }
            catch (Exception ex)
            {
                Console.WriteLine("client:{0}", ex.Message);
            }
            sw.Stop();
            Console.WriteLine($"timer:{sw.Elapsed.TotalMilliseconds}ms");




            Console.ReadKey();
        }
    }
}
